package ui.tp3.TP3Wicket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.ImageButton;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.resource.ContextRelativeResource;

import ui.tp3.modelo.Celda;
import ui.tp3.modelo.Coordenada;
import ui.tp3.modelo.Esfera;
import ui.tp3.modelo.Matriz;
import ui.tp3.modelo.Personaje;
import ui.tp3.modelo.Posicionable;
import ui.tp3.modelo.Tablero;
import ui.tp3.modelo.UserException;

public class AccionesSobreElMapaPage extends WebPage {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Component panelTabl;
	private Tablero tablero;
	private Personaje personajeSelected;
	private Esfera esferaSelected;
	private FeedbackPanel feedbackPanel;
	
	
	

	
	public AccionesSobreElMapaPage(Tablero tabl){
		
		feedbackPanel = new FeedbackPanel("feedback");
		add(feedbackPanel);
		feedbackPanel.setOutputMarkupId(true);
		
		this.setTablero(tabl);
	
		Form formB = new Form("formularioB");
		formB.setOutputMarkupId(true);
		
		Form<Matriz> tableroForm = new Form<Matriz>("formTablero",new CompoundPropertyModel<Matriz>(this.tablero.getMatriz()));
		this.addFilasTabl(tableroForm);
		tableroForm.setOutputMarkupId(true);
		
		this.addSelectors(formB);
		this.addButtons(formB);
		
		this.add(new Link<Object>("crearEsfera"){

			private static final long serialVersionUID = 1L;
			
			@Override
			public void onClick() {
				CreacionEsferasPage crearEsfPage = new CreacionEsferasPage(AccionesSobreElMapaPage.this);
				this.setResponsePage(crearEsfPage);
				
			}
			
    	});
    	
		this.add(new Link<Object>("crearPersonaje"){

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				CreacionPersonajesPage crearPJPage = new CreacionPersonajesPage(AccionesSobreElMapaPage.this);
				this.setResponsePage(crearPJPage);
				
			}
			
    	});
    	
		add(formB);
		add(tableroForm);
	}





	private void addButtons(Form formB) {
		formB.add(new Button("verStatus"){

			private static final long serialVersionUID = 1L;
			
			public void onSubmit() {
				PersonajeStatusPage statusPage = new PersonajeStatusPage(AccionesSobreElMapaPage.this);
				this.setResponsePage(statusPage);
				
			}
			
    	});
		
		formB.add(new Button("buscarEsfera"){

			private static final long serialVersionUID = 1L;
			
			public void onSubmit() {
				try {
						System.out.println(AccionesSobreElMapaPage.this.personajeSelected.getMapa().getEsferas() + "ALALALALALALA111111111");
						AccionesSobreElMapaPage.this.personajeSelected.obtenerEsfera(AccionesSobreElMapaPage.this.esferaSelected);
						System.out.println(AccionesSobreElMapaPage.this.personajeSelected.getMapa().getEsferas() + "ALALALALALALA222222222");
				} catch (UserException e) {
					error(e.getMessage());
				}
			}
			
    	});
		
	}





	private void addSelectors(Form formB) {
		
		formB.add(new DropDownChoice<Personaje>("pers",new PropertyModel<Personaje>(this, "personajeSelected"),  this.tablero.getPersonajes()).setRequired(true));
		formB.add(new DropDownChoice<Esfera>("esf",new PropertyModel<Esfera>(this, "esferaSelected"),  this.tablero.getEsferas()));
		
	}





	private void addFilasTabl(Form<Matriz> tableroForm) {
		this.panelTabl = new PropertyListView<ArrayList<Posicionable>>("celdas") {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<ArrayList<Posicionable>> item) {
				ArrayList<Posicionable> colum = (ArrayList<Posicionable>) item.getModelObject();
				item.add(AccionesSobreElMapaPage.this.addColumTabl(colum, panelTabl));
			}
		};
		tableroForm.add(this.panelTabl);
		
	}





	protected ListView<Posicionable> addColumTabl(ArrayList<Posicionable> colum, Component panelTabl) {
		return new ListView<Posicionable>("columna", colum){
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem item) {
				final Posicionable elem = (Posicionable) item.getModelObject();
				/*if(elem.getClass() == Personaje.class){
					
					item.add(new ImageButton("imgCelda", new ContextRelativeResource("/"+ elem.getNombre()+".jpg")){

						/**
						 * 
						 */
						/*private static final long serialVersionUID = 1L;
						
						public void onSubmit(){
							final ModalWindow modal2;
					        AccionesSobreElMapaPage.this.add(modal2 = new ModalWindow("modal2"));

					        modal2.setTitle("El personaje: " + elem.getNombre() + " tiene las siguiente esferas ");
					        modal2.setCookieName("modal-2");
					        modal2.setPageCreator(new ModalWindow.PageCreator(){
					       
								private static final long serialVersionUID = 1L;

								public Page createPage(){
									return new EsferasPersonajeModalWindow(AccionesSobreElMapaPage.this, modal2);
								}
					        });
					        modal2.show(target);
						}
						
					});
				}*/
				item.add(new Image("imgCelda", new ContextRelativeResource("/"+ elem.getNombre()+".jpg")));
			}
		};	
	}





	public void setTablero(Tablero tablero) {
		this.tablero = tablero;
	}





	public Tablero getTablero() {
		return tablero;
	}

	public void setPersonajeSelected(Personaje personajeSelected) {
		this.personajeSelected = personajeSelected;
	}





	public Personaje getPersonajeSelected() {
		return this.personajeSelected;
	}

	
	

}
